package ru.spb.osll.myfit.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;

import ru.spb.osll.myfit.client.PageMode;
import ru.spb.osll.myfit.client.dialog.ErrorDialog;
import ru.spb.osll.myfit.client.event.PageButtonEvent;
import ru.spb.osll.myfit.client.log.RemoteLogger;
import ru.spb.osll.myfit.client.manager.PageExecutingManager;
import ru.spb.osll.myfit.client.service.FitService;
import ru.spb.osll.myfit.client.service.FitServiceAsync;
import ru.spb.osll.myfit.client.widget.*;
import ru.spb.osll.myfit.common.Page;
import ru.spb.osll.myfit.common.PageElement;
import ru.spb.osll.myfit.common.PageExecutingResult;

import java.util.List;
import java.util.Vector;

public class TestResultsView extends AbstractView {

  public enum ViewMode {
    TABLE, PAGE_RESULT;
  }

  private PageButtonEventListener m_buttonEventListener;
  private final Label m_label = new Label("Tests Results");
  private ResultTableWidget m_tableWidget = new ResultTableWidget();

  private ViewMode m_mode;
  private ResultTableEventListener m_ResultTableEventListener;
  private PageWidget m_pageWidget = new PageWidget(new GWTWidgetFactory());
  private FitServiceAsync m_fitService = GWT.create(FitService.class);
  private DialogBox m_errorDialog = new ErrorDialog();

  private PageExecutingManager m_pageExecutingManager;

  public TestResultsView() {
    m_mode = ViewMode.TABLE;
    m_buttonEventListener = createButtonEventListener();
    m_ResultTableEventListener = createResultTableEventListener();
    m_tableWidget.addResultTableEventListener(m_ResultTableEventListener);
    m_label.addStyleName("testResultTextStyle");
    m_label.setWidth("300px");
    createRootPanel();
  }

  private ResultTableEventListener createResultTableEventListener() {
    ResultTableEventListener listener = new ResultTableEventListener() {

      @Override
      public void actionPerformed(ResultTableEvent event) {
        switch (event.getType()) {
        case OPEN_PAGE_RESULT:
          m_mode = ViewMode.PAGE_RESULT;
          PageExecutingResult m_PageExecutingResult = event.getResult();
          String pageId = m_PageExecutingResult.getID();
          String pagePath = m_PageExecutingResult.getName();
          m_pageWidget.setMode(PageMode.VIEW);
          m_pageWidget.setPagePath(pagePath);
          /* m_pageExecutingManager.addListener(pageId,
              new PageExecutingListenerImpl(m_pageWidget));
          */
          m_fitService.loadPageFromTestResultsDirectory(pagePath, pageId,
              new AsyncCallback<Page>() {

                @Override
                public void onFailure(Throwable caught) {
                  caught.printStackTrace();
                  m_errorDialog.center();
                }

                @Override
                public void onSuccess(Page page) {
                  m_tableWidget.removeFromParent();
                  m_pageWidget.clear();
                  Vector<PageElement> pageElements = new Vector<PageElement>();
                  pageElements = page.getElements();
                  for (PageElement e : pageElements) {
                    m_pageWidget.addPageElementWidget(e);
                  }
                  m_pageWidget.setOpen(true);
                  m_rootPanel.add(m_pageWidget);
                }

              });
        }
      }
    };
    return listener;
  }

  private PageButtonEventListener createButtonEventListener() {
    PageButtonEventListener listener = new PageButtonEventListener() {

      @Override
      public void actionPerformed(PageButtonEvent event) {
        switch (event.getType()) {
        case BACK_TO_TEST_PAGE:
          if (m_mode.equals(ViewMode.TABLE)) {
            Window.open(ViewConstants.START_PAGE, "", "");
          } else {
            m_mode = ViewMode.TABLE;
            m_pageWidget.clear();
            m_pageWidget.removeFromParent();
            m_rootPanel.add(m_tableWidget);
          }
          break;
        }
      }
    };
    return listener;
  }

  @Override
  protected Widget createContentPanel() {
    return m_tableWidget;
  }

  @Override
  protected void createEditPanel() {
    super.createEditPanel();
    m_editPanel.add(m_label);

    m_editPanel.add(createButtonPanel());

  }

  private Widget createButtonPanel() {
    VerticalPanel buttonPanel = new VerticalPanel();
    buttonPanel.setWidth("100%");

    HorizontalPanel hPanel = new HorizontalPanel();
    hPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
    hPanel.setSpacing(5);
    hPanel.add(new PageButton("Back to Test page", "backToTestButtonStyle",
        PageButtonEvent.PageButtonEventType.BACK_TO_TEST_PAGE,
        m_buttonEventListener));
    buttonPanel.add(hPanel);
    return buttonPanel;
  }

  public void addResultToWidget(List<PageExecutingResult> list) {
    if (list != null) {
      for (PageExecutingResult result : list) {
        m_tableWidget.addResult(result);
      }
    }
  }

  public void updateResult(PageExecutingResult result) {
    m_tableWidget.updateResult(result);
  }
}
